package com.example.leetcode.datastream;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

class TwoSum {

    List<Integer> list;
    Set<Integer> sums;

    public TwoSum() {
        this.list = new ArrayList<>();
        this.sums = new HashSet<>();
    }

    public void add(int number) {
        list.add(number);
        for (int i = 0; i < list.size() - 1; i++) {
            sums.add(list.get(i) + number);
        }
    }

    public boolean find(int value) {
        return sums.contains(value);
    }
}

/**
 * Your TwoSum object will be instantiated and called as such:
 * TwoSum obj = new TwoSum();
 * obj.add(number);
 * boolean param_2 = obj.find(value);
 */

public class leetcode170 {
    public static void main(String[] args) {
        TwoSum twoSum = new TwoSum();
        twoSum.add(1);   // [] --> [1]
        twoSum.add(3);   // [1] --> [1,3]
        twoSum.add(5);   // [1,3] --> [1,3,5]
        System.out.println(twoSum.find(4));  // 1 + 3 = 4，返回 true
        System.out.println(twoSum.find(7));  // 没有两个整数加起来等于 7 ，返回 false
    }
}
